﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevTeam;
using System;
using System.Data;
using System.ServiceModel;

namespace ConfExpert.Forms.Ithalat
{
    public partial class UNS_Tickets : XForm
    {
        public UNS_Tickets()
        {
            InitializeComponent();
        }

        private void UNS_Tickets_Load(object sender, EventArgs e)
        {
            gridView1.Init("GetUNS_Ticket");
            gridView1.AddSummary();
            gridView1.BestFitColumns();
            gridView1.MakeReadOnly();

            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            btnGuncelle.PerformClick();
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UnspedWS.KarbelDataWsdlSoapClient unsped = new UnspedWS.KarbelDataWsdlSoapClient();
            string userName = AppSession.UnspedUsername;
            string password = AppSession.UnspedPassword;
            BasicHttpBinding binding = (BasicHttpBinding)unsped.Endpoint.Binding;
            binding.MaxBufferSize = Int32.MaxValue;
            binding.MaxReceivedMessageSize = Int32.MaxValue;
            binding.SendTimeout = new TimeSpan(1, 0, 0);

            DataTable t = new DataTable();
            t.Columns.Add("TicketId", typeof(int));
            t.Columns.Add("Durum", typeof(bool));
            t.Columns.Add("DurumAciklama", typeof(string));

            UnspedWS.CagriDataCevap c = new UnspedWS.CagriDataCevap();
            foreach (DataRow r in gridView1.DataTable().Rows)
            {
                bool Durum = r["Durum"].ToBool();
                if (!Durum)
                {
                    int TicketId = r["UNS_TicketId"].ToInt();
                    unsped.CagriCevap(userName, password, TicketId, out c);
                    
                    DataRow dr = t.NewRow();
                    dr["TicketId"] = TicketId;
                    dr["Durum"] = c.Durum;
                    dr["DurumAciklama"] = c.DurumAciklama;
                    t.Rows.Add(dr);

                }
            }

            if (t.Rows.Count > 0)
                ExecuteNonQuery("UnsUpdTicketStatus2", t.Serialize());

            gridView1.RefreshDataSource("GetUNS_Ticket");
            gridView1.BestFitColumns();
        }
    }
}
